package auth

import (
	"gin_project/config"
	"gin_project/utils/jwt"
	"gin_project/utils/logs"
	"net/http"

	"github.com/gin-gonic/gin"
)

type UserInfo struct {
	Username string `json:username`
	Password string `json:password`
}

//实现登陆逻辑

func Login(c *gin.Context) {
	//获取username password
	userinfo := &UserInfo{}
	if err := c.ShouldBindJSON(userinfo); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "解析错误",
		})
		return
	}
	logs.Debug(map[string]interface{}{"username": userinfo.Username, "password": userinfo.Password}, "开始验证登录信息")

	if userinfo.Username == config.UserName && userinfo.Password == config.PassWord {
		ss, err := jwt.GenToken(userinfo.Username)
		if err != nil {
			c.Header("token", ss)
			c.JSON(http.StatusOK, gin.H{
				"code":    http.StatusOK,
				"message": "登陆成功",
			})
		}
	}
}

func Logout(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "退出成功",
	})
	logs.Debug(nil, "退出成功")
}
